package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: a
 * Date: 2022-08-31
 * Time: 18:37
 */

import java.util.Scanner;

/**
 * 创建两个线程, 一个线程调用wait, 一个线程调用notify
 */
public class Demo8 {
    public static Object locker = new Object();

    public static void main(String[] args) {
        //创建一个用来等待的线程
        Thread waitTask = new Thread(() -> {
            synchronized (locker){
                try {
                    System.out.println("wait之前");
                    locker.wait();
                    System.out.println("wait之后");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        waitTask.start();

        //创建一个用来唤醒的线程
        Thread notifyTask = new Thread(() -> {
            //让用户控制, 输入内容之后再执行唤醒
            Scanner scanner = new Scanner(System.in);
            System.out.println("输入任意内容执行唤醒:");
            scanner.next();  //此处next会发生阻塞,直到输入内容
            synchronized (locker){
                System.out.println("notify开始");
                locker.notify();
                System.out.println("notify结束");
            }
        });
        notifyTask.start();
    }
}
